/* 
组合继承：
JavaScript中最常用的继承模式，
缺陷：组合继承最大的问题就是无论什么情况下，都会调用两次超类型构造函数：一次是在创建子类型原型的时候，另一次是在子类型构造函数内部。
*/

function Person (name, age) {
  this.name = name;
  this.age = age;
}

Person.prototype.say = function() {
  console.log(`I am ${this.name}`);
}

function Worker (name, age, workAge) {
  // 继承属性
  Person.call(this, name, age);
  this.workAge = workAge;
}

// 继承方法
/* Worker.prototype = new Person();
Worker.prototype.constructor = Worker;

Worker.prototype.work = function() {
  console.log(`I have worked for ${this.workAge} years`);
} */

/* 
寄生组合式继承：
开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。
*/
// 继承方法
Worker.prototype = Object(Person.prototype);
Worker.prototype.constructor = Worker;

Worker.prototype.work = function() {
  console.log(`I have worked for ${this.workAge} years`);
}


// test
var person1 = new Person('张三', 18);
person1.say();

var person2 = new Person('李四', 20);
person2.say();

var worker1 = new Worker('王五', 30, 10);
worker1.say();
worker1.work();

var worker2 = new Worker('赵六', 40, 20);
worker2.say();
worker2.work();